package net.pimpas.web.tests.operations;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

import net.pimpas.operation.exception.OperationException;
import net.pimpas.operation.operation.OperationAnnotation;
import net.pimpas.operation.persistence.operation.OperationPersistenceAbstract;

@OperationAnnotation(name="LessThan100Operation")
public class TestInputOperation extends OperationPersistenceAbstract<Object> {
	
	private Object value;

	public void commit() throws OperationException {
		
	}

	public void end() throws OperationException {
		
	}

	public void init(Object... parameters) throws OperationException {
		this.setValue(parameters[0]);
	}

	public void rollBack() throws OperationException {
		
	}

	public void start() throws OperationException {
		
	}

	public Object call() throws Exception {
		String v = (String) this.getValue();
		try {
			Integer it = Integer.parseInt(v);
			if(it >= 100)
				throw new ValidatorException(new FacesMessage("Value ["+v+"] need be less than 100"));
		} catch(NumberFormatException e) {
			throw new ValidatorException(new FacesMessage("Value ["+v+"] is not a Number!"));
		}
		
		return null;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}
}